package com.example.mvvmkotlin.application

import android.app.Application
import com.example.mvvmkotlin.db.DaoMaster
import com.example.mvvmkotlin.db.DaoSession
import com.example.mvvmkotlin.db.initdb.UpgradeDevOpenHelper

class BaseApplication : Application() {
    private lateinit var daoSession: DaoSession
    private val db_name = "user_db"

    companion object {
        lateinit var instance: BaseApplication
        fun instance(): BaseApplication {
            return instance
        }
    }

    override fun onCreate() {
        super.onCreate()
        //初始化数据库
        instance = this
        //1.创建数据库
        val upgradeDevOpenHelper = UpgradeDevOpenHelper(this, db_name)
        //2.获取读写对象
        val daoMaster = DaoMaster(upgradeDevOpenHelper.writableDatabase)
        //3.获取管理器类
        daoSession = daoMaster.newSession()

    }


    @JvmName("getDaoSession1")
    fun getDaoSession(): DaoSession {
        return daoSession
    }
}
